package com.ruoyi.mixuebingcheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mixuebingcheng.domain.bo.ViewAreaBo;
import com.ruoyi.mixuebingcheng.domain.vo.ViewAreaVo;
import com.ruoyi.mixuebingcheng.domain.ViewArea;
import com.ruoyi.mixuebingcheng.mapper.ViewAreaMapper;
import com.ruoyi.mixuebingcheng.service.IViewAreaService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * VIEWService业务层处理
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RequiredArgsConstructor
@Service
public class ViewAreaServiceImpl implements IViewAreaService {

    private final ViewAreaMapper baseMapper;

    /**
     * 查询VIEW
     */
    @Override
    public ViewAreaVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public TableDataInfo<ViewAreaVo> queryPageList(ViewAreaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ViewArea> lqw = buildQueryWrapper(bo);
        Page<ViewAreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public List<ViewAreaVo> queryList(ViewAreaBo bo) {
        LambdaQueryWrapper<ViewArea> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ViewArea> buildQueryWrapper(ViewAreaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ViewArea> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProvinceName()), ViewArea::getProvinceName, bo.getProvinceName());
        lqw.like(StringUtils.isNotBlank(bo.getCityName()), ViewArea::getCityName, bo.getCityName());
        lqw.like(StringUtils.isNotBlank(bo.getDistrictName()), ViewArea::getDistrictName, bo.getDistrictName());
        return lqw;
    }

    /**
     * 新增VIEW
     */
    @Override
    public Boolean insertByBo(ViewAreaBo bo) {
        ViewArea add = BeanUtil.toBean(bo, ViewArea.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改VIEW
     */
    @Override
    public Boolean updateByBo(ViewAreaBo bo) {
        ViewArea update = BeanUtil.toBean(bo, ViewArea.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ViewArea entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除VIEW
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
